package 我的Java学习_算法基础.day_05;

import java.util.HashMap;

public class _074_判断两字符串的字符集是否相同 {
    public static void main(String[] args) {
        String s1 = "abcdd",s2="abcd";
        System.out.println(check(s1,s2));
    }
    private static boolean check(String s1,String s2){
        HashMap<Character,Integer> hashMap = new HashMap();
        //检查s2中出现的元素种类s1中是否都有
        for(int i = 0;i<s1.length();i++){
            char c = s1.charAt(i);
            if(hashMap.get(c)==null){
                hashMap.put(c,1);
            }
        }
        for(int i = 0;i<s2.length();i++){
            char c = s2.charAt(i);
            if(hashMap.get(c)==null){
                return false;
            }
        }
        hashMap.clear();
        //检查s1中出现的元素种类s2中是否都有
        for(int i = 0;i<s2.length();i++){
            char c = s2.charAt(i);
            if(hashMap.get(c)==null){
                hashMap.put(c,1);
            }
        }
        for(int i =0;i<s1.length();i++){
            char c = s1.charAt(i);
            if(hashMap.get(c)==null){
                return false;
            }
        }
        return true;
    }
    private static boolean check2(String s1,String s2){
        int[] helper1 = new int[128];
        int[] helper2 = new int[128];
        //创建辅助1
        for (int i =0;i<s1.length();i++) {
            int t = (int) (s1.charAt(i));
            helper1[t] = 1;
        }
        //字符串2与辅助1比对，即检查s2中出现的元素种类s1中是否都有
        for(int i = 0;i<s2.length();i++){
            int t = (int) (s2.charAt(i));
            if(helper1[t]==0){
                return false;
            }
        }
        //创建辅助2
        for (int i =0;i<s2.length();i++){
            int t = (int)(s2.charAt(i));
            helper2[t]=1;
        }
        //字符串1与辅助2比对，即检查s1中出现的元素种类s2中是否都有
        for(int i = 0;i<s1.length();i++){
            int t = (int) (s1.charAt(i));
            if(helper2[t]==0){
                return false;
            }
        }
        return true;

    }
}
